from deepseek_api import DeepseekAPI
import speech_recognition as sr
from pydub import AudioSegment
import io
from wxauto import WeChat

class ChatHandler:
    def __init__(self):
        self.api = DeepseekAPI()
        self.recognizer = sr.Recognizer()
        self.wx = WeChat()
    
    def handle_message(self, msg_type, content, sender):
        """处理消息并返回简洁的回复"""
        try:
            current_msg = str(content)
            print(f"当前消息: {current_msg}")
            
            # 如果是AI的回复，不处理
            if current_msg.startswith("AI："):
                print("这是AI的回复，跳过处理")
                return None
            
            # 处理用户消息
            if msg_type == "text":
                response = self.api.chat(current_msg)
                return response
            elif msg_type == "voice":
                text = self.voice_to_text(content)
                if text:
                    response = self.api.chat(text)
                    return response
                return "抱歉，我没有听清楚"
            else:
                return "请用文字告诉我您的问题"
            
        except Exception as e:
            print(f"处理消息时发生错误：{str(e)}")
            return None 